package com.example.redis6.controller;

import com.example.redis6.dao.VideoDao;
import com.example.redis6.model.VideoDo;
import com.example.redis6.util.JsonData;
import com.example.redis6.util.JsonUtil;
import com.example.redis6.vo.CartItemVo;
import com.example.redis6.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 * description: CartController <br>
 * date: 2021/7/21 10:08 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("api/v1/cart")
public class CartController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private VideoDao videoDao;

    @RequestMapping("add")
    public JsonData addCart(int videoId,int buyNum){

        // 获取购物车信息
        BoundHashOperations<String, Object, Object> myCartOps = getMyCartOps();
        Object cacheObj = myCartOps.get(videoId + "");

        String result = null;
        if (cacheObj != null) {
            result = (String) cacheObj;
        }

        if (cacheObj == null){
            // 属于新加入购物车的商品
            CartItemVo cartItemVo = new CartItemVo();
            VideoDo videoDo = videoDao.findDetailById(videoId);

            cartItemVo.setPrice(videoDo.getPrice());
            cartItemVo.setBuyNum(buyNum);
            cartItemVo.setProductId(videoDo.getId());
            cartItemVo.setProductImg(videoDo.getImage());
            cartItemVo.setProductTitle(videoDo.getTitle());

            myCartOps.put(videoId+"", JsonUtil.objectToJson(cartItemVo));
        }else {
            // 增加数量
            CartItemVo cartItemVo = JsonUtil.jsonToPojo(result, CartItemVo.class);
            cartItemVo.setBuyNum(cartItemVo.getBuyNum()+buyNum);

            myCartOps.put(videoId+"",JsonUtil.objectToJson(cartItemVo));
        }

        return JsonData.buildSuccess();

    }

    /**
     * 购物车详情
     * @return
     */
    @RequestMapping("getCart")
    public JsonData getMyCart(){
        // 获取购物车
        BoundHashOperations<String, Object, Object> myCartOps = getMyCartOps();

        List<Object> itemList = myCartOps.values();

        List<CartItemVo> cartItemVoList = new ArrayList<>();

        // 将从Redis读取的数据转换成对象装入list
        for (Object item : itemList) {
            CartItemVo cartItemVo = JsonUtil.jsonToPojo((String) item, CartItemVo.class);
            cartItemVoList.add(cartItemVo);
        }
        CartVo cartVo = new CartVo();
        cartVo.setCartItems(cartItemVoList);

        return JsonData.buildSuccess(cartVo);

    }

    /**
     * 清空购物车
     * @return
     */
    @RequestMapping("clear")
    public JsonData clearCart(){
        String cartKey = getCartKey();
        redisTemplate.delete(cartKey);
        return JsonData.buildSuccess();
    }

    /**
     * 购物车获取通用方法
     * @return
     */
    private BoundHashOperations<String ,Object,Object> getMyCartOps(){
        String key = getCartKey();
        return redisTemplate.boundHashOps(key);
    }

    private String getCartKey(){
        int userId = 88;
        String cartKey = String.format("video:cart:%s",userId);
        return cartKey;
    }

}
